package com.macro.mall.tiny.modules.ums.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.ums.dto.UmsAdminLoginParam;
import com.macro.mall.tiny.modules.ums.dto.UmsAdminParam;
import com.macro.mall.tiny.modules.ums.dto.UmsMenuNode;
import com.macro.mall.tiny.modules.ums.dto.UpdateAdminPasswordParam;
import com.macro.mall.tiny.modules.ums.model.UmsAdmin;
import com.macro.mall.tiny.modules.ums.model.UmsRole;
import com.macro.mall.tiny.modules.ums.service.UmsAdminService;
import com.macro.mall.tiny.modules.ums.service.UmsRoleService;
import com.macro.mall.tiny.security.util.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * 后台用户管理
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "ComptureController", description = "多线程异步返回数据")
@RequestMapping("/compture")
public class ComptureController {

    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private UmsRoleService roleService;


    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> test() {
        long l = System.currentTimeMillis();
        ThreadPoolTaskExecutor executor = SpringUtil.getBean(ThreadPoolTaskExecutor.class);
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        List<CompletableFuture<Void>> all = new ArrayList<>();
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, executor);
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, executor);
        all.add(future);
        all.add(future1);
        if (CollUtil.isNotEmpty(all)) {
            CompletableFuture.allOf(all.toArray(new CompletableFuture[]{})).join();
        }
        long l1 = System.currentTimeMillis() - l;
        System.out.println("耗时:"+l1);
        return CommonResult.success("dd");

    }


    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(10);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,5,10, TimeUnit.SECONDS,workQueue,new TestThreadFactory(),new MyRejectPolicy());
        List<CompletableFuture<Void>> all = new ArrayList<>();
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, executor);
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, executor);
        all.add(future);
        all.add(future1);
        if (CollUtil.isNotEmpty(all)) {
            CompletableFuture.allOf(all.toArray(new CompletableFuture[]{})).join();
        }

//        try {
//            TimeUnit.SECONDS.sleep(5);
//            TimeUnit.SECONDS.sleep(5);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        long l1 = System.currentTimeMillis() - l;
        System.out.println("耗时:"+l1);
    }







}
